package io.renren.modules.app.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.modules.app.annotation.Login;
import io.renren.modules.app.annotation.LoginUser;
import io.renren.modules.app.entity.ImApplyEntity;
import io.renren.modules.app.entity.ImFriendEntity;
import io.renren.modules.app.entity.UserEntity;
import io.renren.modules.app.service.ImFriendService;
import io.renren.modules.app.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 *
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-04-26 13:24:53
 */
@RestController
@RequestMapping("app/imfriend")
public class ImFriendController {
    @Autowired
    private ImFriendService imFriendService;
    @Autowired
    private UserService userService;

    /**
     * 验证好友信息
     * @param user
     * @return
     */
    @Login
    @RequestMapping("/check")
    //@RequiresPermissions("generator:imapply:list")
    public R list(@LoginUser UserEntity user , @RequestBody ImFriendEntity imFriend){
        ImFriendEntity ifend = imFriendService.getOne(new QueryWrapper<ImFriendEntity>().eq("isblack", 0).eq("friend_id", user.getUserId()).eq("user_id",imFriend.getUserId()));
        if (ifend==null)return R.error("对方不存在或者已经把你拉黑");

        if((userService.getOne(new QueryWrapper<UserEntity>().eq("user_id", imFriend.getUserId()))).getStatus()!=0)return R.error("对方已被禁用");;

        return R.ok();
    }
    /**
     * 列表
     */
    @Login
    @RequestMapping("/list")
    //@RequiresPermissions("generator:imfriend:list")
    public R list(@LoginUser UserEntity user ,@RequestParam Map<String, Object> params){
        PageUtils page = imFriendService.queryPage(params);
        Map<String, List<ImFriendEntity>> list = imFriendService.getListById(user);
        return R.ok(list);
    }

    /**
     * 设置/取消星标好友
     */
    @Login
    @RequestMapping("/setremarktag")
    //@RequiresPermissions("generator:imfriend:list")
    public R setremarktag(@LoginUser UserEntity user ,@RequestBody UserEntity bean){
        //PageUtils page = imFriendService.queryPage(params);
        R r = imFriendService.setremarktag(user,bean);
        return r;
    }

    /**
     * 移入/移除黑名单
     */
    @Login
    @RequestMapping("/setblack")
    //@RequiresPermissions("generator:imfriend:list")
    public R setblack(@LoginUser UserEntity user ,@RequestBody UserEntity bean){
        //PageUtils page = imFriendService.queryPage(params);
        R r = imFriendService.setblack(user,bean);
        return r;
    }

    /**
     * 设置朋友圈权限
     */
    @Login
    @RequestMapping("/setMomentAuth")
    //@RequiresPermissions("generator:imfriend:list")
    public R setMomentAuth(@LoginUser UserEntity user ,@RequestBody UserEntity bean){
        //PageUtils page = imFriendService.queryPage(params);
        R r = imFriendService.setMomentAuth(user,bean);
        return r;
    }

    /**
     * // 设为星标朋友
     */
    @Login
    @RequestMapping("/setstar")
    //@RequiresPermissions("generator:imfriend:list")
    public R setstar(@LoginUser UserEntity user ,@RequestBody ImFriendEntity bean){
        //PageUtils page = imFriendService.queryPage(params);
        R r = imFriendService.setstar(user,bean);
        return r;
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("generator:imfriend:info")
    public R info(@PathVariable("id") Integer id){
		ImFriendEntity imFriend = imFriendService.getById(id);

        return R.ok().put("imFriend", imFriend);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("generator:imfriend:save")
    public R save(@RequestBody ImFriendEntity imFriend){
		imFriendService.save(imFriend);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("generator:imfriend:update")
    public R update(@RequestBody ImFriendEntity imFriend){
		imFriendService.updateById(imFriend);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:imfriend:delete")
    public R delete(@RequestBody Integer[] ids){
		imFriendService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
